<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

//Place code here
global $action; 
function list_table(){
    global $db,$page,$pagesize,$login_status,$user_id,$cat;
     
    if (empty($page)){
        $start=0;
        ;
    } else {
        $start=$page * $pagesize;
        
    }
    if (empty($cat)){
        $sql="select a.*,b.category_name 
        from _channel_channels a,_channel_category b
        where a.channel_category=b.id
        order by a.id limit $start,$pagesize "; 
    } else {
    $sql="select a.*,b.category_name 
        from _channel_channels a,_channel_category b
        where a.channel_category=b.id and a.channel_category=$cat
        order by a.id limit $start,$pagesize";
    }
    $result=$db->query($sql);
    //echo "<table id=\"table_box\" cellpadding=2 cellspacing=0><tr valign=\"top\">";
    $col=0;
    echo "<h3>Daftar Channel</h3>
        <div align=\"right\"><a href=\"?mod=panel\"><img class=\"icon\" src=\"images/panel.png\" title=\"Back to Panel\"/></a>
<a href=\"?mod=panel&file=channel&action=add\"><img class=\"icon\" src=\"images/panel/add_channel.png\" title=\"Add Channel\"/></a></div>        
<div id=\"channel_list\"><table width=\"100%\" cellspacing=0 cellpadding=2>
<tr><th>No</th><th>Nama</th><th>Description</th><th>Action</th></tr>
";
    $i=0;
    while ($row=$db->fetch_array($result)){
        extract($row);
        if ($login_status){
        $editlink="";
        }
        $i++; 
        echo "<tr ><td class=\"table_row_left\">$i</td>
        <td class=\"table_row\"><a href=\"?mod=panel&file=contents&channel_id=$id\" 
        title=\"List Channel Content\">$channel_name</a></td><td class=\"table_row\">
        <div id=\"description_$id\">$channel_description</div></td><td class=\"table_row\">
        <a href=\"?mod=panel&file=channel&action=edit&id=$id\">
    <img class=icon src=\"images/edit.png\" title=\"Edit\" /></a>
    <a class=\"delete_confirm\" href=\"?mod=panel&file=channel&action=delete&id=$id\">
    <img class=icon src=\"images/delete.png\" title=\"Hapus\" /></a>
        </td>  </tr>
       ";
         
    }
    $jrow=mysql_num_rows($result);
    if ($jrow<1){
        echo "<tr><td colspan='4' class=\"table_row\">There is no record here</td></tr>";
    }
    echo "</table></div>";
}
function edit_form(){
    global $db,$id;
    if (!empty($id)){
        $sql="select * from _channel_channels where id=$id";
        $row=$db->fetch_row($sql);
        if (!empty($row)){
            extract($row);
            echo "<form action=\"?mod=panel&file=channel\" method=\"post\">
            <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
            <table>
            <tr><td colspan=2><h3>Edit Channel</h3> </td></tr>
<tr><td>Id</td><td><input type=\"hidden\" name=\"id\" value=\"$id\"/> </td></tr>
<tr><td>Channel Name</td><td><input type=\"text\" size=\"50\" name=\"channel_name\" class=\"textbox\" value=\"$channel_name\"/> </td></tr>
<tr><td>Channel Description</td>
            <td><textarea name=\"channel_description\" rows=\"5\" cols=\"50\"
            class=\"textbox\">$channel_description</textarea>
             </td></tr>
<tr><td>Channel Category</td><td>";
            select_channel_category($channel_category);
            echo "</td></tr>
<tr><td>Channel Image</td><td><input type=\"text\" name=\"channel_image\" 
class=\"textbox\" size=\"50\" value=\"$channel_image\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
        }
    }
}
function edit_process(){
    global $db,$id,$channel_name,$channel_description,$channel_category,$channel_image;
    if (!empty($id)){
        $sql="update _channel_channels set channel_name='$channel_name',
        channel_description='$channel_description',
        channel_category=$channel_category,
        channel_image='$channel_image' where id=$id";
        $db->query($sql);
        refresh_to("?mod=panel&file=channel", "Kembali ke daftar Channel", 1);
    }
}
function add_form(){
    echo "<form action=\"?mod=panel&file=channel\" method=\"post\">
            <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
            <table>
<tr><td colspan=2><h3>Menambahkan Channel</h3> </td></tr>
<tr><td>Channel Name</td><td><input type=\"text\" size=\"50\" name=\"channel_name\" class=\"textbox\" value=\"$channel_name\"/> </td></tr>
<tr><td>Channel Description</td>
            <td><textarea name=\"channel_description\" rows=\"5\" cols=\"50\"
            class=\"textbox\">$channel_description</textarea>
             </td></tr>
<tr><td>Channel Category</td><td>";
            select_channel_category('');
            echo "</td></tr>
<tr><td>Channel Image</td><td><input type=\"text\" name=\"channel_image\" 
class=\"textbox\" size=\"50\" value=\"$channel_image\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Kirim\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
function add_process(){
    global $db,$channel_name,$channel_description,$channel_category,$channel_image;
    if (!empty($channel_name)){
        $sql="insert into _channel_channels values (null,'$channel_name',
        '$channel_description',$channel_category,'$channel_image')";
        $db->query($sql);
        refresh_to("?mod=panel&file=channel", "Kembali ke daftar Channel", 1);
    }
}
function delete(){
    global $db,$id;
    $channel_name=get_channel_info($id, 'channel_name');
    $msg= "Channel $channel_name is deleted!";
    $db->query("delete from _channel_channels where id=$id");
    refresh_to("?mod=panel&file=channel", $msg, 2);
}
switch($action){
    case 'add_process':
        add_process();
        break;
    case 'add':
        add_form();
        break;
    case 'edit':
        edit_form();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'delete':
        delete();
        break;
    default:
        list_table();
        break;
}
?>
<div id="dialog" title="Confirmation">
  Are you sure to delete this channel?
</div>
<script type="text/javascript">
  $(document).ready(function() {
    $("#dialog").dialog({
      autoOpen: false,
      modal: true
    });
  });

  $(".delete_confirm").click(function(e) {
    e.preventDefault();
    var targetUrl = $(this).attr("href");

    $("#dialog").dialog({
      buttons : {
        "Confirm" : function() {
          window.location.href = targetUrl;
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog").dialog("open");
  });
</script>
<p align="center">
<a href="javascript:history.go(-1);"><img src="images/back_button.png"/></a></p>